package request

import (
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"lab.com/consts"
	"lab.com/define/errorcode"
)

// AchievementListReq 科研成果列表查询参数
type AchievementListReq struct {
	Title     string `json:"title" form:"title" comment:"关键字"`
	Type      int    `json:"type" form:"type" validate:"oneof=0 10 20 30 40 50 60" comment:"科研成果类型"`
	IsAll     int    `json:"is_all" form:"is_all" validate:"oneof=0 10" comment:"是否是全部，0全部 10首页"`
	StartTime string `json:"start_time" form:"start_time" comment:"开始时间"`
	EndTime   string `json:"end_time" form:"end_time" comment:"结束时间"`
	PageOffset
}

// AchievementBasicInfo 科研成果列表信息
type AchievementBasicInfo struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Abstract    string `json:"abstract"`
	Type        int    `json:"type"`
	Author      string `json:"author"`
	ImgPath     string `json:"img_path"`
	PublishTimt string `json:"publish_time"`
}

// AchievementListRes 科研成果列表返回结果
type AchievementListRes struct {
	List  []AchievementBasicInfo `json:"list"`
	Count int                    `json:"count"`
}

// CheckInputParams return input params
func (ach *AchievementListReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(ach); err != nil {
		return err
	}
	if errs, err := Validate(ach); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	ach.ParseOffset()
	return nil
}

// AchievementDetailReq 科研成果详情查询参数
type AchievementDetailReq struct {
	ID int `json:"id" form:"id" validate:"required" comment:"科研成果id"`
}

// AchievementDetailRes 科研成果详情返回结果
type AchievementDetailRes struct {
	AchievementInfo
	Content string `json:"content"`
}

// CheckInputParams return input params
func (ach *AchievementDetailReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(ach); err != nil {
		return err
	}
	if errs, err := Validate(ach); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	return nil
}

// AchievementAddReq 科研成果新增参数
type AchievementAddReq struct {
	Title       string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	Abstract    string `json:"abstract" form:"abstract" validate:"required,max=100" comment:"摘要"`
	Author      string `json:"author" form:"author" validate:"required,max=20" comment:"作者"`
	Content     string `json:"content" form:"content" comment:"内容"`
	Type        int    `json:"type" form:"type" validate:"oneof=10 20 30 40 50 60" comment:"科研成果类型"`
	PublishTime string `json:"publish_time" form:"publish_time" validate:"required" comment:"发布时间"`
	IsShow      int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	IsHome      int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	ImgPath     string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
}

// CheckInputParams return input params
func (ach *AchievementAddReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(ach); err != nil {
		return err
	}
	if errs, err := Validate(ach); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// AchievementDeleteReq 科研成果删除参数
type AchievementDeleteReq struct {
	ID int `json:"id" form:"id" validate:"required" comment:"科研成果id"`
}

// CheckInputParams return input params
func (ach *AchievementDeleteReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(ach); err != nil {
		return err
	}
	if errs, err := Validate(ach); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// AchievementEditReq 科研成果编辑参数
type AchievementEditReq struct {
	ID          int    `json:"id" form:"id" validate:"required" comment:"科研成果id"`
	Title       string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	Abstract    string `json:"abstract" form:"abstract" validate:"required,max=100" comment:"摘要"`
	Author      string `json:"author" form:"author" validate:"required,max=20" comment:"作者"`
	Content     string `json:"content" form:"content" comment:"内容"`
	Type        int    `json:"type" form:"type" validate:"oneof=10 20 30 40 50 60" comment:"科研成果类型"`
	PublishTime string `json:"publish_time" form:"publish_time" validate:"required" comment:"发布时间"`
	IsShow      int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	IsHome      int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	ImgPath     string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
}

// CheckInputParams return input params
func (ach *AchievementEditReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(ach); err != nil {
		return err
	}
	if errs, err := Validate(ach); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// AchievementShowReq 科研成果展示参数
type AchievementShowReq struct {
	ID     int `json:"id" form:"id" validate:"required" comment:"科研成果id"`
	IsShow int `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
}

// CheckInputParams return input params
func (ach *AchievementShowReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(ach); err != nil {
		return err
	}
	if errs, err := Validate(ach); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// AchievementHomeReq 科研成果首页展示参数
type AchievementHomeReq struct {
	ID     int `json:"id" form:"id" validate:"required" comment:"科研成果id"`
	IsHome int `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
}

// CheckInputParams return input params
func (ach *AchievementHomeReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(ach); err != nil {
		return err
	}
	if errs, err := Validate(ach); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// AchievementListInManageReq 科研成果管理列表查询参数
type AchievementListInManageReq struct {
	Title string `json:"title" form:"title" comment:"关键字"`
	Type  int    `json:"type" form:"type" validate:"oneof=0 10 20 30 40 50 60" comment:"科研成果类型"`
	// IsShow    int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	// IsHome    int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	StartTime string `json:"start_time" form:"start_time" comment:"开始时间"`
	EndTime   string `json:"end_time" form:"end_time" comment:"结束时间"`
	PageOffset
}

// AchievementInfo 科研成果列表信息
type AchievementInfo struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Abstract    string `json:"abstract"`
	Type        int    `json:"type"`
	Author      string `json:"author"`
	ImgPath     string `json:"img_path"`
	IsShow      int    `json:"is_show"`
	IsHome      int    `json:"is_home"`
	PublishTime string `json:"publish_time"`
}

// AchievementListInManageRes 科研成果列表返回结果
type AchievementListInManageRes struct {
	List  []AchievementInfo `json:"list"`
	Count int               `json:"count"`
}

// CheckInputParams return input params
func (ach *AchievementListInManageReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(ach); err != nil {
		return err
	}
	if errs, err := Validate(ach); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}

	ach.ParseOffset()
	return nil
}
